/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.msg.hrappy.web.servlet;

import edu.msg.hrappy.backend.model.Candidate;
import edu.msg.hrappy.backend.model.Job;
import edu.msg.hrappy.backend.model.User;
import edu.msg.hrappy.backend.service.CandidateService;
import edu.msg.hrappy.backend.service.JobService;
import edu.msg.hrappy.backend.service.UserService;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ShowCandidateServlet extends HttpServlet {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    @EJB
    private JobService jobService;
    @EJB
    private UserService userService;
    @EJB
    private CandidateService candidateService;

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {

        HttpSession session = req.getSession();
        session.setAttribute("jobId", req.getParameter("jobId"));
        User user = userService.getUserByUserName(req.getUserPrincipal()
                .toString());
        System.out.println(user.getRoleInCompany());
        session.setAttribute("userId", user.getId());
        session.setAttribute("username", user.getUserName());

        Job job = jobService.getById(Long.parseLong(req.getParameter("jobId")));
        session.setAttribute("job", job);
        List<Candidate> candidates = job.getCandidates();
        session.setAttribute("candidates", candidates);
        String address = "./showCandidates.jsp";

        RequestDispatcher dispatcher = req.getRequestDispatcher(address);

        dispatcher.forward(req, resp);

    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        HttpSession session = req.getSession();
        session.setAttribute("jobId", req.getParameter("jobId"));
        User user = userService.getUserByUserName(req.getUserPrincipal()
                .toString());
        session.setAttribute("userId", user.getId());
        session.setAttribute("username", user.getUserName());

        Job job = jobService.getById(Long.parseLong(req.getParameter("jobId")));
        Candidate candidate = candidateService.getById(Long.parseLong(req.getParameter("candId")));
        String accept = req.getParameter("accept");
        String ref = req.getParameter("ref");
        if (accept != null) {
            candidate.setAccept(1);
            job.setNrOfPositions(job.getNrOfPositions()-1);
            if (job.getNrOfPositions()==0){
                job.setActive(false);
            }
            candidate.setJob(job);
            candidateService.update(candidate);
            jobService.update(job);
        } else if (ref != null) {
            candidate.setAccept(2);
            candidateService.update(candidate);
        }
        session.setAttribute("job", job);
        List<Candidate> candidates = job.getCandidates();
        session.setAttribute("candidates", candidates);
        String address = "./showCandidates.jsp";

        RequestDispatcher dispatcher = req.getRequestDispatcher(address);

        dispatcher.forward(req, resp);
    }
}
